在本项目中,我选取了《摩拜单车上海用户数据集》,对摩拜单车的用户数据进行了一系列分析和探索,得出了一些关于用户行程和订单的结论。
通过载入文件,对文件进行目测和编程分析,得到数据视觉化中需用到的深层信息
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sb
%matplotlib inline
import datetime
import plotly_express as px
#根据第一次review的反馈,为了使用dt.day_name(),本次更新了pandas版本为最新版并在自己电脑上进行测试,更新的版本是1.1.1,测试成功
pd.__version__
#载入文件
df1 = pd.read_csv('mobike_shanghai_sample_updated.csv')
#了解文件内数据的基本信息
df1.info()
#查看是否有重复条目
sum(df1.duplicated())
分离开始时间
#将start_time转换为时间日期格式,并将月份,日期进行分离,提取出这一天是星期几
df1['start_time'] = pd.to_datetime(df1['start_time'])
df1['start_month']=df1['start_time'].dt.month
df1['start_hour']=df1['start_time'].dt.hour
df1['start_day_of_week']=df1['start_time'].dt.day_name()
分离结束时间,执行与上面相同的操作
df1['end_time'] = pd.to_datetime(df1['end_time'])
df1['end_month']=df1['end_time'].dt.month
df1['end_hour']=df1['end_time'].dt.hour
#检查结果
df1.head()
计算出每个行程持续的时长
#将end_time与start_time相减,以分钟为单位
df1['duration_min'] = (df1['end_time']-df1['start_time']).dt.seconds/60
#检查结果
df1['duration_min'].head()
df1['duration_min'].value_counts()
统计星期几产生的订单最多呢?
#周末(Saturday & Sunday)竟然没有排进前三名
df1.start_day_of_week.value_counts()
将出发和目的地的经纬度进行拼接,为可视化做准备
df1.start_location_x = df1.start_location_x.astype(str)
df1.start_location_y = df1.start_location_y.astype(str)
df1.end_location_x = df1.end_location_x.astype(str)
df1.end_location_y = df1.end_location_y.astype(str)
df1['start_location'] = df1['start_location_x'].str.cat(df1['start_location_y'],sep=',')
df1['end_location'] = df1['end_location_x'].str.cat(df1['end_location_y'],sep=',')
# 计算订单量
plt.title('On Which Day Are Most Orders Issued')
plt.xlabel('Start Day of Week')
plt.ylabel('Orders')
df1.start_day_of_week.value_counts().sort_values().plot(kind='barh');
# 持续时长的探索
plt.title('How Long Will Most Riding Last?')
plt.xlabel('Total Number of User')
plt.ylabel('Length of Riding(min)')
df1.duration_min.value_counts().iloc[0:10].sort_values().plot(kind='barh');
#因为上面统计中,大部分行程都在10分钟以内,所以这里稍稍扩大范围,取到30分钟来制作小提琴图进行观察
df_short_duration = df1[df1['duration_min']<30]
#使用小提琴图探索星期几和行程时长之间的关系
sb.violinplot(data=df_short_duration,x='start_day_of_week',y='duration_min',color=sb.color_palette()[0]);
plt.title('Relationship of Duration and the Week of Day');
plt.xlabel('Start Day of Week')
plt.ylabel('The Duration of Riding (min)')
根据第一版审阅反馈,尝试使用了plotly_express(第一次接触px感觉好神奇,顺便又复习了一下group()和size(),啊,要学的还有太多了)
# 计算每个 start location 的数量,并合并到原数据集中(又学会了一个计算数量的方法)
df_count = df1.groupby(['start_location']).size().rename('start_location_count').reset_index()
df1 = df1.merge(df_count,on='start_location',how='left')
df1.head()
# 设定地图的中心坐标(这里试着运行了这段代码,发现做出这2个地图后页面有点卡顿)
center = {'lat':31.25, 'lon':121.4}
# 绘制气泡图
px.scatter_mapbox(df1, lat='start_location_y', lon='start_location_x', zoom=10, center = center, size='start_location_count', opacity=0.1, mapbox_style='open-street-map')
# 绘制热图
px.density_mapbox(df1, lat='start_location_y', lon='start_location_x', z='start_location_count', radius=10, center=center, zoom=10, mapbox_style="open-street-map")